package com.sz.announce.dao.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sz.announce.dao.IAnnounceDAO;
import com.sz.common.ICommonDAO;

public class AnnounceDAO extends SqlMapClientDaoSupport implements IAnnounceDAO {
	
	private ICommonDAO commonDAO = null;

	public ICommonDAO getCommonDAO() {
		return commonDAO;
	}

	public void setCommonDAO(ICommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}
	
	@SuppressWarnings("unchecked")
	public long addAnnounce_business(Map map) throws Exception {
		Object obj = getSqlMapClient().insert("announce_sqlmap.insert_announce_business", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	
	@SuppressWarnings("unchecked")
	public long addAnnounce_seating(Map map) throws Exception {
		Object obj = getSqlMapClient().insert("announce_sqlmap.insert_announce_seating", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	
	@SuppressWarnings("unchecked")
	public String uptAnnounce_seating(Map map) throws Exception {

		
		
		
		String announce_seating_id = map.get("ANNOUNCE_SEATING_ID").toString();
		String state = map.get("STATE").toString();
		String title = map.get("TITLE").toString();
		String content = map.get("CONTENT").toString();
		String begin_time = map.get("BEGIN_TIME").toString();
		String end_time = map.get("END_TIME").toString();
		//String create_time = map.get("CREATE_TIME").toString();
		//String create_manager = map.get("CREATE_MANAGER").toString();

		
		String sql = String.format("select * from announce_seating where announce_seating_id = %s", announce_seating_id);
		Map r = (Map)commonDAO.queryForObject(sql);		
		if(r == null) return "查询不到坐席公告";	
		
		
		

		Map param = new HashMap();
		param.put("ANNOUNCE_SEATING_ID", announce_seating_id);
		param.put("STATE", state);
		param.put("TITLE", title);
		param.put("CONTENT", content);
		param.put("BEGIN_TIME", begin_time);
		param.put("END_TIME", end_time);
		param.put("CREATE_TIME", r.get("CREATE_TIME"));
		param.put("CREATE_MANAGER", r.get("CREATE_MANAGER"));


		getSqlMapClient().update("announce_sqlmap.update_announce_seating", param);
		return "修改成功";
	}
	
	@SuppressWarnings("unchecked")
	public String delAnnounce_seating(Map map) throws Exception {

		Map param = new HashMap();
		param.put("ANNOUNCE_SEATING_ID", map.get("ANNOUNCE_SEATING_ID").toString());
		getSqlMapClient().delete("announce_sqlmap.delete_announce_seating", param);
		return "删除成功";
			
	}
	
	@SuppressWarnings("unchecked")
	public List queryAnnounce_business_avalid(Map map) throws Exception {
		return getSqlMapClient().queryForList("announce_sqlmap.query_announce_business_avalid", map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryAnnounce_seating(Map map) throws Exception {
		return getSqlMapClient().queryForList("announce_sqlmap.query_announce_seating", map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryAnnounce_seatingCount(Map map) throws Exception {
		Map r = (Map)getSqlMapClient().queryForObject("announce_sqlmap.query_announce_seating_count", map);
		return Long.parseLong(r.get("COUNT").toString());
	}

	@SuppressWarnings("unchecked")
	public List queryAnnounce_seatingPage(Map map) throws Exception {
		List list = getSqlMapClient().queryForList("announce_sqlmap.query_announce_seating_page", map);
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		for(Iterator it=list.iterator();it.hasNext();)
		{
			Map mapTemp=(Map)it.next();
			mapTemp.put("BEGIN_TIME", format.format(mapTemp.get("BEGIN_TIME")));
			mapTemp.put("END_TIME", format.format(mapTemp.get("END_TIME")));
			mapTemp.put("CREATE_TIME", format.format(mapTemp.get("CREATE_TIME")));
		
			if("1".equals(mapTemp.get("STATE").toString()))
			{
				mapTemp.put("STATE", "有效");
			}
			else
			{
				mapTemp.put("STATE", "无效");
			}	
				
			String sql = String.format("select NAME from MANAGER where MANAGER_ID = %s", mapTemp.get("CREATE_MANAGER").toString());
			
			Map r = (Map)commonDAO.queryForObject(sql);	
			if(r != null) 
			{
				mapTemp.put("CREATE_MANAGER", r.get("NAME").toString());
			}
			
		}
		return list;
	}
	
	public List queryAnnounce_seatingShow() throws Exception {

		return getSqlMapClient().queryForList("announce_sqlmap.query_announce_seating_show");
	}
}
